<?php

    /**
     * Created by PhpStorm.
     * User: Administrator
     * Date: 2015/5/27 0027
     * Time: 上午 2:27
     */
    class Layout
    {

        static public $obj;
        static public $data = [ ];
        var $layout;
        static public $layout_view = [ ];

        /**
         * 装载视图
         *
         * @param array $layout
         * @param null  $data
         */
        function Layout( array $layout = [ ], array $data = [ ] )
        {
            if ( self::$data ) {
                self::$data = array_merge( self::$data, $data );
            } else {
                self::$data = $data;
            }

            if ( !self::$obj ) {
                self::$obj =& get_instance();
            }
            self::$layout_view = array_merge( $layout, self::$layout_view );
        }

        /**
         * 调用视图
         *
         * @param      $view
         * @param null $data
         * @param bool $return
         *
         * @return mixed
         */
        function view( $view, $data = null, $return = false )
        {
            if ( !is_array( $data ) ) {
                $data = [ $data ];
            }

            if ( $return ) {
                $output = self::$obj->load->view( $view, $data, true );

                return $output;
            }

            if ( !is_array( $view ) ) {
                $view = [ 'content_for_layout' => $view ];
            }
            //装载视图
            self::Layout( $view, $data );

            $last_layout = end( self::$layout_view );
            //循环输出模板
            foreach ( self::$layout_view as $k => $v ) {
                self::$data[ $k ] = self::$obj->load->view( $v, self::$data, true );
            }
            self::$obj->load->view( $last_layout, self::$data );
        }

    }